package com.weavernorth.action.AssistantReport;

import com.weavernorth.sysap.dao.SyncOrgStrucDaoImpl;
import org.apache.commons.logging.Log;


import weaver.conn.RecordSet;
import weaver.formmode.setup.ModeRightInfo;
import weaver.general.Util;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;

import java.util.UUID;

/**
 * 店员考核评分转入报表
 */

public class AssistantReport implements Action {
    private static Log log = org.apache.commons.logging.LogFactory.getLog(AssistantReport.class.getName());
    @Override
    public String execute(RequestInfo requestInfo) {
        RecordSet rsSql = new RecordSet();
        RecordSet rsAsstantSql = new RecordSet();
        RecordSet rsUpdateFw = new RecordSet();
        RecordSet rsUpdateLs = new RecordSet();
        RecordSet rsUpdateCl = new RecordSet();
        RecordSet rsWc = new RecordSet();
        RecordSet rs = new RecordSet();
        RecordSet rsJs = new RecordSet();
        log.info("逻辑开始");

        //1：获取表单的requestid
        String strRequestId = requestInfo.getRequestid();
        log.info("表单requestid"+strRequestId);
        //2：获取表单名称tablename
        String strTablename = requestInfo.getRequestManager().getBillTableName();
        log.info("表单名称："+strTablename);
        //查询表单数据的sql
        String strSql = "select *   from  "+strTablename+" f,"+strTablename+"_dt1  d  where  d.mainid=f.id  and  f.requestid = '"+strRequestId+"'";
        log.info("查询表单主表、明细数据的sql:"+strSql);
        rsSql.executeQuery(strSql);
        //流程可能含有多个明细表
        while(rsSql.next()){
        //获取姓名字段值
        String strXm = Util.null2String(rsSql.getString("xm") );
        log.info("姓名："+strXm);

        //获取评分类型字段的值
        String strPflx = Util.null2String(rsSql.getString("pflx"));
        log.info("评分类型："+strPflx);
        //获取总得分字段的值
        double  douZdf = Util.getDoubleValue(rsSql.getString("zdf"),0.0);
        log.info("总得分："+douZdf);

        //获取分公司的值
        String  strFgs = Util.null2String(rsSql.getString("fgs"));
        log.info("分公司："+strFgs);
        //获取奖金金额
        //查询奖金基数
        String strJsSql = "select dyjs from uf_jjjspzb where  fgs = '"+strFgs+"'";
        rsJs.executeQuery(strJsSql);
        String strJs="0.0";
        if(rsJs.next()){
            //获取奖金基数
             strJs = Util.null2String(rsJs.getString("dyjs"));
        }


        //获取店铺名称的值
        String  strDpmc = Util.null2String(rsSql.getString("dpmc"));
        log.info("店铺名称："+strDpmc);
        //获取职位值
            String  strZw = Util.null2String(rsSql.getString("zw"));
            log.info("职位："+strZw);
        //获取工号值
            String  strGh = Util.null2String(rsSql.getString("gh"));
            log.info("工号："+strGh);
        //获取零售经理的值
            String  strLsjl = Util.null2String(rsSql.getString("lsjl"));
            log.info("零售经理："+strLsjl);
        //获取区域经理的值
            String  strQyjl = Util.null2String(rsSql.getString("qyjl"));
            log.info("区域经理："+strQyjl);
        //获取品牌的值
            String  strPp = Util.null2String(rsSql.getString("pp"));
            log.info("品牌："+strPp);
        //获取店铺类型的值
            String  strDplx = Util.null2String(rsSql.getString("dplx"));
            log.info("店铺类型："+strDplx);
        //获取店铺级别的值
            String  strDpjb = Util.null2String(rsSql.getString("dpjb"));
            log.info("店铺级别："+strDpjb);
        //获取店铺状态的值
            String  strDpzt = Util.null2String(rsSql.getString("dpzt"));
            log.info("店铺状态："+strDpzt);
        //获取评分期间的值
            String  strPfqj = Util.null2String(rsSql.getString("pfqj"));
            log.info("评分期间："+strPfqj);
        //获取评分日期的值
            String  strPfrq = Util.null2String(rsSql.getString("pfrq"));
            log.info("评分日期："+strPfrq);
        //查询建模表：店员考核评分汇总表中 是否有 此人对应的数据
        String  strAssistantSql = "select  *  from  uf_dypfhzb  where  xm ='"+strXm+"' and  pfqj = '"+strPfqj+"'" ;
            log.info("判断建模表中有无此人数据的sql："+strAssistantSql);
        rsAsstantSql.executeQuery(strAssistantSql);
        //有此人
        if(rsAsstantSql.next()){
            //获取服务标准的分数
            double douFwbz = Util.getDoubleValue(rsAsstantSql.getString("fwbzpf"),0.0);
            log.info("有此人时，其服务标准分数："+douFwbz);
            //获取零售流程的分数
            double douLs = Util.getDoubleValue(rsAsstantSql.getString("lsyypf"),0.0);
            log.info("有此人时，其零售管理分数："+douLs);
            //获取陈列管理的分数
            double douClgl = Util.getDoubleValue(rsAsstantSql.getString("clglpf"),0.0);
            log.info("有此人时，其陈列管理分数："+douClgl);
            //获取总分数
            double douZfs = Util.getDoubleValue(rsAsstantSql.getString("zfs"),0.0);
            //此数据的id
            String strId=Util.null2String(rsAsstantSql.getString("id"));

            //判断评分类型为服务标准流程
            if(strPflx.equals("2")){
                //服务标准人员总评分
                double douFwbzSum=douZdf+douFwbz;
                //获取奖金金额
                double douDf = (douFwbzSum + douLs + douClgl);
                double dubJj = (douDf / 100) * Double.parseDouble(strJs);
                double dubDev = (double) Math.round(dubJj*100) / 100;
                String strUpdateAstat = "update uf_dypfhzb set  fwbzpf = '"+douFwbzSum+"',zfs='"+douDf+"', jjje = '"+dubDev+"' where id='"+strId+"'";
                log.info("更新服务标准流程的sql:"+strUpdateAstat);
                rsUpdateFw.executeQuery(strUpdateAstat);
            }
            //判断评分类型为零售管理流程
            if(strPflx.equals("1")){
                //零售体验人员总评分
                double douLstySum=douZdf+douLs;
                //获取奖金金额
                double douDf = douLstySum+douFwbz+douClgl;
                double dubJj = (douDf / 100) * Double.parseDouble(strJs);
                double dubDev = (double) Math.round(dubJj*100) / 100;
                String strUpdateAstat = "update uf_dypfhzb set  lsyypf = '"+douLstySum+"',zfs='"+douDf+"', jjje = '"+dubDev+"' where id='"+strId+"'";
                log.info("更新零售管理流程的sql:"+strUpdateAstat);
                rsUpdateLs.executeQuery(strUpdateAstat);
            }
            //判断评分类型为陈列管理流程
            if(strPflx.equals("3")){
                //陈列管理个人总评分
                double douClglSum=douZdf+douClgl;
                //获取奖金金额
                double douDf = douClglSum+douFwbz+douLs;
                double dubJj = (douDf / 100) * Double.parseDouble(strJs);
                double dubDev = (double) Math.round(dubJj*100) / 100;
                String strUpdateAstat = "update uf_dypfhzb set  clglpf = '"+douClglSum+"',zfs='"+douDf+"', jjje = '"+dubDev+"' where id='"+strId+"'";
                log.info("更新陈列管理流程的sql:"+strUpdateAstat);
                rsUpdateCl.executeQuery(strUpdateAstat);
            }
            //无此人
        }else{
            //判断评分类型为服务标准流程
            if(strPflx.equals("2")){
                //获取奖金金额
               // double intDf = Integer.parseInt(strZdf);
                double dubJj = (douZdf / 100) * Double.parseDouble(strJs);
                double dubDev = (double) Math.round(dubJj*100) / 100;
                //获取uuid,授权需要的参数
                String strUuid=  UUID.randomUUID().toString().replaceAll("-", "");
                log.info("uuid:"+strUuid);
                //获取formmodeid
                int intModeId = SyncOrgStrucDaoImpl.getModeIdByTableName("uf_dypfhzb");
                log.info("服务标准流程的formmodeid:"+intModeId);
                String strInsertAstat = "insert  into  uf_dypfhzb   (fwbzpf,zfs,fgs,dpmc,xm,zw,gh,lsjl,qyjl,pp,dplx,dpjb,dpzt,pfqj,formmodeid,uuid,pfrq,jjje) " +
                                                                       "values ('"+douZdf+"','"+douZdf+"','"+strFgs+"','"+strDpmc+"','"+strXm+"','"+strZw+"','"+strGh+"','"+strLsjl+"','"+strQyjl+"','"+strPp+"','"+strDplx+"','"+strDpjb+"','"+strDpzt+"','"+strPfqj+"','"+intModeId+"','"+strUuid+"','"+strPfrq+"','"+dubDev+"')";
                log.info("新增服务标准流程的sql:"+strInsertAstat);
                rsUpdateFw.executeQuery(strInsertAstat);
                //授权
                int intMaxId = 0;
                rs.executeQuery("select id from uf_dypfhzb where uuid='"+strUuid+"'");
                if (rs.next()) {
                    intMaxId = Util.getIntValue((rs.getString("id")), 0);
                }
                log.info("授权id:"+intMaxId);
                ModeRightInfo modeRightInfoBanc = new ModeRightInfo();
                modeRightInfoBanc.setNewRight(true);
                modeRightInfoBanc.editModeDataShare(1, intModeId, intMaxId);
            }
            //判断评分类型为零售管理流程
            if(strPflx.equals("1")){
                //获取奖金金额
              //  double intDf = Integer.parseInt(strZdf);
                double dubJj = (douZdf / 100) * Double.parseDouble(strJs);
                double dubDev = (double) Math.round(dubJj*100) / 100;
                //获取uuid,授权需要的参数
                String strUuid=  UUID.randomUUID().toString().replaceAll("-", "");
                log.info("uuid:"+strUuid);
                //获取formmodeid
                int intModeId = SyncOrgStrucDaoImpl.getModeIdByTableName("uf_dypfhzb");
                log.info("零售流程的formmodeid:"+intModeId);
                String strInsertAstat = "insert  into  uf_dypfhzb   (lsyypf,zfs,fgs,dpmc,xm,zw,gh,lsjl,qyjl,pp,dplx,dpjb,dpzt,pfqj,formmodeid,uuid,pfrq,jjje) values ('"+douZdf+"','"+douZdf+"','"+strFgs+"','"+strDpmc+"','"+strXm+"','"+strZw+"','"+strGh+"','"+strLsjl+"','"+strQyjl+"','"+strPp+"','"+strDplx+"','"+strDpjb+"','"+strDpzt+"','"+strPfqj+"','"+intModeId+"','"+strUuid+"','"+strPfrq+"','"+dubDev+"')";
                log.info("新增零售管理流程的sql:"+strInsertAstat);
                rsUpdateLs.executeQuery(strInsertAstat);
                //授权
                int intMaxId = 0;
                rs.executeQuery("select id from uf_dypfhzb where uuid='"+strUuid+"'");
                if (rs.next()) {
                    intMaxId = Util.getIntValue((rs.getString("id")), 0);
                }
                log.info("授权id:"+intMaxId);
                ModeRightInfo modeRightInfoBanc = new ModeRightInfo();
                modeRightInfoBanc.setNewRight(true);
                modeRightInfoBanc.editModeDataShare(1, intModeId, intMaxId);
            }
            //判断评分类型为陈列管理流程
            if(strPflx.equals("3")){
                //获取奖金金额
                //double intDf = Integer.parseInt(strZdf);
                double dubJj = (douZdf / 100) * Double.parseDouble(strJs);
                double dubDev = (double) Math.round(dubJj*100) / 100;
                //获取uuid,授权需要的参数
                String strUuid=  UUID.randomUUID().toString().replaceAll("-", "");
                log.info("uuid:"+strUuid);
                //获取formmodeid
                int intModeId = SyncOrgStrucDaoImpl.getModeIdByTableName("uf_dypfhzb");
                log.info("陈列管理流程的formmodeid:"+intModeId);
                String strInsertAstat = "insert  into  uf_dypfhzb   (clglpf,zfs,fgs,dpmc,xm,zw,gh,lsjl,qyjl,pp,dplx,dpjb,dpzt,pfqj,formmodeid,uuid,pfrq,jjje) values ('"+douZdf+"','"+douZdf+"','"+strFgs+"','"+strDpmc+"','"+strXm+"','"+strZw+"','"+strGh+"','"+strLsjl+"','"+strQyjl+"','"+strPp+"','"+strDplx+"','"+strDpjb+"','"+strDpzt+"','"+strPfqj+"','"+intModeId+"','"+strUuid+"','"+strPfrq+"','"+dubDev+"')";
                log.info("新增陈列管理流程的sql:"+strInsertAstat);
                rsUpdateCl.executeQuery(strInsertAstat);
                //授权
                int intMaxId = 0;
                rs.executeQuery("select id from uf_dypfhzb where uuid='"+strUuid+"'");
                if (rs.next()) {
                    intMaxId = Util.getIntValue((rs.getString("id")), 0);
                }
                log.info("授权id:"+intMaxId);
                ModeRightInfo modeRightInfoBanc = new ModeRightInfo();
                modeRightInfoBanc.setNewRight(true);
                modeRightInfoBanc.editModeDataShare(1, intModeId, intMaxId);
            }
        }

        }
        log.info("逻辑结束");
        return SUCCESS;
    }
}
